# -*- coding: utf-8 -*-

"""Unimail
@version: 0.14
@license: GPL 3.0 (see "license" file)
@author: Jose A. Jimenez
@contact: jajimc@gmail.com"""

import smtplib
import excep

def check_host(host, tls, port, timeout=10):
    """Checks if a given SMTP host is available.
    @param host: Host address
    @type host: C{str}
    @param tls: Whether to use TLS or not
    @type tls: C{bool}
    @param port: Host port
    @type port: C{int}
    @param timeout: Timeout in seconds to do the check. It must be greater or equal than 1. If omitted, its value is 10.
    @type timeout: C{int}
    @return: Whether if host is available or not
    @rtype: C{bool}"""
    if type(host) != str: raise Exception("'host' must be a string.")
    if type(tls) != bool: raise Exception("'tls' must be a boolean.")
    if type(port) != int or port < 0: raise Exception("'port' must be an integer greater or equal than 0.")
    if timeout == None or type(timeout) != int or timeout < 1: timeout = 10
    
    available = False
    signal.signal(signal.SIGALALRM, excep.process_timeout)
    signal.alarm(timeout)
    
    try:
        connection = smtplib.SMTP(host, port)
        connection.ehlo()
        
        if tls:
            connection.starttls()
            connection.ehlo()
        
        connection.close()
        available = True
    except:
        pass
    
    return available
    
def check_credentials(host, tls, port, userId, userPassword, timeout=10):
    """Checks if a user-password pair is valid for an SMTP server.
    @param host: SMTP host address
    @type host: C{str}
    @param tls: Whether to use TLS or not
    @type tls: C{bool}
    @param port: SMTP host port
    @type port: C{int}
    @param userId: User ID
    @type userId: C{str}
    @param userPassword: User password
    @type userPassword: C{str}
    @param timeout: Timeout in seconds to do the check. It must be greater or equal than 1. If omitted, its value is 10.
    @type timeout: C{int}
    @return: Whether if user-password pair is valid
    @rtype: C{bool}"""
    if type(host) != str: raise Exception("'host' must be a string.")
    if type(tls) != bool: raise Exception("'tls' must be a boolean.")
    if type(port) != int or port < 0: raise Exception("'port' must be an integer greater or equal than 0.")
    if type(userId) != str or userId == "": raise Exception("'userId' must be a non empty string.")
    if type(userPassword) != str or userPassword == "": raise Exception("'userPassword' must be a non empty string.")
    if timeout == None or type(timeout) != int or timeout < 1: timeout = 10
    
    valid = False
    signal.signal(signal.SIGALALRM, excep.process_timeout)
    signal.alarm(timeout)
    
    try:
        connection = smtplib.SMTP(host, port)
        connection.ehlo()
        
        if tls:
            connection.starttls()
            connection.ehlo()
        
        connection.login(userId, userPassword)
        connection.close()
        valid = True
    except:
        pass
        
    return valid
